package com.chj.controller;

import com.chj.config.IdWorker;
import com.chj.entity.DictionaryEntity;
import com.chj.service.DictionaryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class DictionaryController {

        @Autowired
        private DictionaryService dictionaryService;

        @GetMapping("/test/{index}")
        public ResponseEntity<?> test(@PathVariable("index") int index) {
            try {
                DictionaryEntity dictionary = new DictionaryEntity();
                IdWorker worker = new IdWorker();
                dictionary.setName("mybatis-name-" + worker.nextId());
                dictionary.setValue("mybatis-value-" + worker.nextId());
                Long id = dictionaryService.add(dictionary, index);
                return ResponseEntity.ok(id);
            } catch (Exception e) {
                log.error(e.getMessage());
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
            }
        }

}
